<?

#domain status 0:nochange 1:new 2:updated 3:deleted

include("settings.php");
include ("topmenu.php");
echo "<br><br><br><br><br>";

class MySQL_db
{
        var $connection;
        //Constructor
        function MySQL_db (){
	        //Connnect to the database
        	$this->connection= mysql_pconnect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
        	mysql_select_db(DB_NAME,$this->connection) or die(mysql_error());
        }


	function new_domain() {

	//Get POST variables
        //Check magic_quotes enabled or not dont add an extra back slash

   //Check magic_quotes enabled or not dont add an extra back slash

        if (!get_magic_quotes_gpc()) {

                $serial=trim($_POST['serial']);
                $default_ttl=trim($_POST['default_ttl']);
                $email=trim($_POST['email']);
                $nsserver=trim($_POST['nsserver']);
                $ns1=trim($_POST['ns1']);
                $ns2=trim($_POST['ns2']);
                $ns3=trim($_POST['ns3']);
                $ns4=trim($_POST['ns4']);
                $ns5=trim($_POST['ns5']);
                $domainname=trim($_POST[domainname]);
                }
        else
                {
                $domainname=trim( addslashes($_POST[domainname]) );
                $serial=trim( addslashes($_POST['serial']) );
                $default_ttl=trim( addslashes($_POST['default_ttl']) );
                $email=trim( addslashes($_POST['email']) );
                $nsserver=trim( addslashes($_POST['nsserver']) );
                $ns1=trim( addslashes($_POST['ns1']) );
                $ns2=trim( addslashes($_POST['ns2']) );
                $ns3=trim( addslashes($_POST['ns3']) );
                $ns4=trim( addslashes($_POST['ns4']) );
                $ns5=trim( addslashes($_POST['ns5']) );

           }


	
	//Before getting variables check domain existence
	$domain_check_query="select domainname from ".DOMAIN_TABLE." where domainname='$domainname'";
	$num=mysql_query($domain_check_query,$this->connection);
		if (mysql_num_rows($num)!= 0) {
			echo "<center><table><tr><td class=topbar><center><b>$domainname</b> exists on the database. You cannot add it again <a href=newdomain.php>Add Another</a></center></td></tr></table></center>";
		exit();
			}

	

	//Check user input for the last dot
	if (preg_match("/\.$/",$domainname)) {
	        $domainname=preg_replace("/\.$/","",$domainname);
	       }
	if (preg_match("/\.$/",$nsserver)) {
	                $nsserver=preg_replace("\.$","",$nsserver);
	    }
	
	if (preg_match("/\.$/",$ns1)) {
	                $ns1=preg_replace("\.$","",$ns1);
	}
	if (preg_match("/\.$/",$ns2)) {
	                        $ns2=preg_replace("\.$","",$ns2);
				        }
		if (preg_match("/\.$/",$ns3)) {
	                        $ns3=preg_replace("\.$","",$ns3);
			        }
		if (preg_match("/\.$/",$ns4)) {
	                        $ns4=preg_replace("\.$","",$ns4);
			        }
		if (preg_match("/\.$/",$ns5)) {
			       $ns5=preg_replace("\.$","",$ns5);
				 }
																	

	//If user enters an email containing AT change it into dot
        if (preg_match("/\@/",$email)) {
		$email=preg_replace("/\@/",".",$email);
	}
	
	
//Insert also new status flag into all of dns_client_status fields.

$query="INSERT into ".DOMAIN_TABLE." 
  (id,domainname,serial,default_ttl,email,nsserver,status,
  dns_client1_status,dns_client2_status,dns_client3_status,dns_client4_status,dns_client5_status
  )
  VALUES 
  ('','$domainname','$serial','$default_ttl','$email','$nsserver','1',
  '1','1','1','1','1'
  )" ;

$result= mysql_query($query,$this->connection);


//Get ID of new domain from db table
$query="select id from ".DOMAIN_TABLE." where domainname='$domainname'";
$result=mysql_query($query,$this->connection);
	$row=mysql_fetch_assoc($result);
	$domainid=$row['id'];

//Create NS ARRAYS
$ns_array=array('ns1','ns2','ns3','ns4','ns5');

	for ($i=0; $i <= count($ns_array) ; $i++ ) {
 	//if the ns string is not empty
 		if (${$ns_array[$i]}!="") {
     		$q_ns="insert into " .RRS_TABLE. " VALUES('','$domainid','$domainname','$ns_ttl','$domainname','NS','','".${$ns_array[$i]}."','','','')";
     			$result_ns= mysql_query($q_ns,$this->connection);
   		}
   
	}

//FIELD1
	$rrvar1=$_POST['rrvar1'];
	$rrtype1=$_POST['rrtype1'];
	$priority1=$_POST['priority1'];
	$rrvalue1=$_POST['rrvalue1'];
	$ttl1=$_POST['ttl1'];
	if (preg_match("/\.$/",$rrvar1)) {
	      $rrvar1=preg_replace("\.$","",$rrvar1);
        }
        if (preg_match("/\.$/",$rrvalue1)) {
	      $rrvalue1=preg_replace("\.$","",$rrvalue1);
              }
			      
	
	if ($rrvar1!="") {
		if ($rrtype1 == "MX0") { 
		$priority1 = 0 ; 
		$rrtype1="MX";  
		} 
		if ($rrtype1 == "MX1") {
                $priority1 = 1 ;
                $rrtype1= "MX";
                }
		if ($rrtype1 == "MX5") {
		$priority1 = 5 ;
		$rrtype1= "MX";
		}
                if ($rrtype1 == "MX10") {
                $priority1 = 10 ;
                $rrtype1= "MX";
                }
                if ($rrtype1 == "MX20") {
                $priority1 = 20 ;
                $rrtype1= "MX";
                }
                if ($rrtype1 == "SRV_5_0_5269") {
                $priority1 = "5 0 5269" ;
                $rrtype1= "SRV";
                }
                if ($rrtype1 == "SRV_20_0_5269") {
                $priority1 = "20 0 5269" ;
                $rrtype1= "SRV";
                }



		
	if ($rrtype1 == "A" || $rrtype1 == "TXT" || $rrtype1 == "NS" ) {
	     $priority1==NULL;
              }
						
		
		
		$q1="insert into " .RRS_TABLE. " VALUES('','$domainid','$domainname','$ttl1','$rrvar1','$rrtype1','$priority1','$rrvalue1','','','')";
		$result1= mysql_query($q1,$this->connection);
        }

	//FIELD2
	$rrvar2=$_POST['rrvar2'];
	        $rrtype2=$_POST['rrtype2'];
		        $priority2=$_POST['priority2'];
			        $rrvalue2=$_POST['rrvalue2'];
				$ttl2=$_POST['ttl2'];
				
	if ($rrvar2!="") {
	
                if ($rrtype2 == "MX0") {
                $priority2 = 0 ;
                $rrtype2="MX";
                }
		if ($rrtype2 == "MX1") {
                $priority2 = 1 ;
                $rrtype2="MX";
                }
                if ($rrtype2 == "MX5") {
                $priority2 = 5 ;
                $rrtype2= "MX";
                }
                if ($rrtype2 == "MX10") {
                $priority2 = 10 ;
                $rrtype2= "MX";
                }
                if ($rrtype2 == "MX20") {
                $priority2 = 20 ;
                $rrtype2= "MX";
                }
                if ($rrtype2 == "SRV_5_0_5269") {
                $priority3 = "5 0 5269" ;
                $rrtype2= "SRV";
                }
                if ($rrtype2 == "SRV_20_0_5269") {
                $priority2 = "20 0 5269" ;
                $rrtype2= "SRV";
                }


		if ($rrtype2 == "A" || $rrtype2 == "TXT" || $rrtype2 == "NS" ) {
			$priority2==NULL;
		}
		
	if (preg_match("/\.$/",$rrvar2)) {
	              $rrvar2=preg_replace("\.$","",$rrvar2);
		              }
	 if (preg_match("/\.$/",$rrvalue2)) {
                      $rrvalue2=preg_replace("\.$","",$rrvalue2);
                  }
								  


        	$q2="insert into " .RRS_TABLE. " VALUES('','$domainid','$domainname','$ttl2','$rrvar2','$rrtype2','$priority2','$rrvalue2','','','')";
        	$result2= mysql_query($q2,$this->connection);
	
	}
	
	//FIELD3
	$rrvar3=$_POST['rrvar3'];
	        $rrtype3=$_POST['rrtype3'];
		        $priority3=$_POST['priority3'];
			        $rrvalue3=$_POST['rrvalue3'];
				$ttl3=$_POST['ttl3'];
				
	if ($rrvar3!="") {
	
                if ($rrtype3 == "MX0") {
                $priority3 = 0 ;
                $rrtype3="MX";
                }
                if ($rrtype3 == "MX1") {
                $priority3 = 1 ;
                $rrtype3="MX";
                }
                if ($rrtype3 == "MX5") {
                $priority3 = 5 ;
                $rrtype3= "MX";
                }
                if ($rrtype3 == "MX10") {
                $priority3 = 10 ;
                $rrtype3= "MX";
                }
                if ($rrtype3 == "MX20") {
                $priority3 = 20 ;
                $rrtype3= "MX";
                }
                if ($rrtype3 == "SRV_5_0_5269") {
                $priority3 = "5 0 5269" ;
                $rrtype3= "SRV";
                }
                if ($rrtype3 == "SRV_20_0_5269") {
                $priority3 = "20 0 5269" ;
                $rrtype3= "SRV";
                }



	if ($rrtype3 == "A" || $rrtype3 == "TXT" || $rrtype3 == "NS" ) {
	        $priority3==NULL;
	        }
						
	if (preg_match("/\.$/",$rrvar3)) {
	                      $rrvar3=preg_replace("\.$","",$rrvar3);
			                                    }
             if (preg_match("/\.$/",$rrvalue3)) {
                    $rrvalue3=preg_replace("\.$","",$rrvalue3);
                     }
													     


        $q3="insert into " .RRS_TABLE. " VALUES('','$domainid','$domainname','$ttl3','$rrvar3','$rrtype3','$priority3','$rrvalue3','','','')";
        $result3= mysql_query($q3,$this->connection);

        }



	//FIELD 4 BEGIN
        $rrvar4=$_POST['rrvar4'];
                $rrtype4=$_POST['rrtype4'];
                        $priority4=$_POST['priority4'];
                                $rrvalue4=$_POST['rrvalue4'];
                                $ttl4=$_POST['ttl4'];

        if ($rrvar4!="") {

                if ($rrtype4 == "MX0") {
                $priority4 = 0 ;
                $rrtype4="MX";
                }
                if ($rrtype4 = "MX1") {
                $priority4 = 1 ;
                $rrtype4="MX";
                }
                if ($rrtype4 == "MX5") {
                $priority4 = 5 ;
                $rrtype4= "MX";
                }
                if ($rrtype4 == "MX10") {
                $priority4 = 10 ;
                $rrtype4= "MX";
                }
                if ($rrtype4 == "MX20") {
                $priority4 = 20 ;
                $rrtype4= "MX";
                }
                if ($rrtype4 == "SRV_5_0_5269") {
                $priority4 = "5 0 5269" ;
                $rrtype4= "SRV";
                }
                if ($rrtype4 == "SRV_20_0_5269") {
                $priority4 = "20 0 5269" ;
                $rrtype4= "SRV";
                }



                if ($rrtype4 == "A" || $rrtype4 == "TXT" || $rrtype4 == "NS" ) {
                        $priority4==NULL;
                }

        if (preg_match("/\.$/",$rrvar4)) {
                      $rrvar4=preg_replace("\.$","",$rrvar4);
                              }
         if (preg_match("/\.$/",$rrvalue4)) {
                      $rrvalue4=preg_replace("\.$","",$rrvalue4);
                  }



        $q4="insert into " .RRS_TABLE. " VALUES('','$domainid','$domainname','$ttl4','$rrvar4','$rrtype4','$priority4','$rrvalue4','','','')";
                $result4= mysql_query($q4,$this->connection);

        }

	//FIELD 4 END


        echo"
	<center>
	<form method=post action=manage.php>

	<table cellpadding=0 cellspacing=0>
	<tr>
	<td class=topbar><b>
	".$domainname."</b> is added into the database and will be active in a few minutes</b>
	
	</td>
	<td class=topbar><input class=submitlink type=submit name=submit value=ListAllDomains></td></tr>
	</table>
	</form>
	</center>
	";
	
	}
	


	function list_all_secondery($page_no){
	        require('settings.php');
		require('lang.php');
		$q = "SELECT domainname from ".SECDNS_TABLE;
        	$result = mysql_query($q,$this->connection);
		
		
        //Lets decide howmany pages we must display	
	$total_domain = mysql_num_rows($result);
	
	if (($total_domain % $domainsperpage ) != 0 ) {
		   $maxpage = ( $total_domain) / DOMAINS_PERPAGE  + 1 ;
		}
	    else
		{
		   $maxpage = ($total_domain) / DOMAINS_PERPAGE ;
		}

	//We found the number of pages
	$maxpage = (int) $maxpage;

        $domain_no=0;

	  
          echo "<form action=secdns.php method=POST><center>
	        <table cellspacing=0 cellpadding=0 style='margin-bottom: 1px;'>
                <tr>
		<td class=header><b>No</b></td><td class=header><center><b>domainname</b></center></td><td class=header><center>Remove</cen
ter></td><td class=header><center>UpdateZONE</center></td></tr>
	  
                <tr><td class=top1><center>Search</center></td><td class=top1><center><input type=text name=searchthisdomain></center></td>
<td class=top1><input type=submit class=submitlink name=submit value=\"Look Up\"></td><td class=top1><center>Total domain: $total_domain</c
enter></td></tr><br>
  
                </form>
  
               ";
       
		$searchthisdomain=$_POST[searchthisdomain];
       
	 	//New Query to be able to view per page style 
		//Before that, check that there is a search for the domain
		
	        if ($searchthisdomain=="") {
		 $q_page = "SELECT id,domainname,status from ".SECDNS_TABLE." where id >".DOMAINS_PERPAGE*($page_no-1)." LIMIT ".DOMAINS_PERPAGE;
		 $result=mysql_query($q_page,$this->connection);
		
		 }
		 else
		 {
		 $q_domain= "SELECT id,domainname,status from ".SECDNS_TABLE." where domainname LIKE '%$searchthisdomain%'";
		 $result=mysql_query($q_domain,$this->connection);
		 }

		 	
		
	 
	 while ($row=mysql_fetch_assoc($result)) {
	 	$domain_no++;
         		echo "<form method=POST action=secdns.php>";
          		echo "<tr>";
			if ($row[status]==1) {
			  //This is a new domain
        		  echo "<td class=header>".$row[id]."</td><td class=newdomain>".$row['domainname']." *$new*</td><td class=list1>"; 
			} else
			{
			  echo "<td class=header>".$row[id]."</td><td class=list1>".$row['domainname']."</td><td class=list1>";
			 }
			
			//If the domain is marked for removal dont show remove button again
			if ($row[status] == 3) { 
			
		     echo "<font color=red><center>REMOVED</center></font></td><td class=list1><font color=red><center>REMOVED</center></fo
nt></td>";
		     	    }
	                else	
		           {
		     //If it is  removed show remove button
		     echo "	<input type=hidden name=domainname value=".$row['domainname'].">
			<input type=submit class=submitlink name=submit value=remove></td>";
			//Column4
			echo "<td class=list1><center><input type=submit class=submitlink name=submit value=DisplaySecondery></center></td>
";
			

		}
	          
	    echo "</tr>";
	    echo "</form>";

          }
	 
	    echo "</table>";
	   
		
			if ($searchthisdomain=="") {
					
				echo "<form action=secdns.php method=POST>
				<center><input type=submit class=submitlink name=submit value=\"Display Page\">";
				echo "<select name=page_no>";
				for ($i=1; $i<=$maxpage+1 ; $i++) {
				echo "<option value=".$i.">Page no: ".$i;
				}
				echo "</center></form>";

			}
			

        }



	function list_all_domains($page_no){
	        require('settings.php');
		require('lang.php');
		$q = "SELECT domainname from ".DOMAIN_TABLE;
        	$result = mysql_query($q,$this->connection);
		
		
        //Lets decide howmany pages we must display	
	$total_domain = mysql_num_rows($result);
	
	if (($total_domain % $domainsperpage ) != 0 ) {
		   $maxpage = ( $total_domain) / DOMAINS_PERPAGE  + 1 ;
		}
	    else
		{
		   $maxpage = ($total_domain) / DOMAINS_PERPAGE ;
		}

	//We found the number of pages
	$maxpage = (int) $maxpage;

        $domain_no=0;

	  
          echo "<form action=manage.php method=POST><center>
	        <table cellspacing=0 cellpadding=0 style='margin-bottom: 1px;'>
                <tr>
		<td class=header><b>No</b></td><td class=header><center><b>domainname</b></center></td><td class=header><center>Remove</center></td><td class=header><center>UpdateZONE</center></td></tr>
	  
                <tr><td class=top1><center>Search</center></td><td class=top1><center><input type=text name=searchthisdomain></center></td><td class=top1><input type=submit class=submitlink name=submit value=\"Look Up\"></td><td class=top1><center>Total domain: $total_domain</center></td></tr><br>
  
                </form>
  
               ";
       
		$searchthisdomain=$_POST[searchthisdomain];
       
	 	//New Query to be able to view per page style 
		//Before that, check that there is a search for the domain
		
	        if ($searchthisdomain=="") {
		 $q_page = "SELECT id,domainname,status from ".DOMAIN_TABLE." where id >".DOMAINS_PERPAGE*($page_no-1)." LIMIT ".DOMAINS_PERPAGE;
		 $result=mysql_query($q_page,$this->connection);
		
		 }
		 else
		 {
		 $q_domain= "SELECT id,domainname,status from ".DOMAIN_TABLE." where domainname LIKE '%$searchthisdomain%'";
		 $result=mysql_query($q_domain,$this->connection);
		 }

		 	
		
	 
	 while ($row=mysql_fetch_assoc($result)) {
	 	$domain_no++;
         		echo "<form method=POST action=manage.php>";
          		echo "<tr>";
			if ($row[status]==1) {
			  //This is a new domain
        		  echo "<td class=header>".$row[id]."</td><td class=newdomain>".$row['domainname']." *$new*</td><td class=list1>"; 
			} else
			{
			  echo "<td class=header>".$row[id]."</td><td class=list1>".$row['domainname']."</td><td class=list1>";
			 }
			
			//If the domain is marked for removal dont show remove button again
			if ($row[status] == 3) { 
			
		     echo "<font color=red><center>REMOVED</center></font></td><td class=list1><font color=red><center>REMOVED</center></font></td>";
		     	    }
	                else	
		           {
		     //If it is  removed show remove button
		     echo "	<input type=hidden name=domainname value=".$row['domainname'].">
			<input type=submit class=submitlink name=submit value=remove></td>";
			//Column4
			echo "<td class=list1><center><input type=submit class=submitlink name=submit value=DisplayDomain></center></td>";
			

		}
	          
	    echo "</tr>";
	    echo "</form>";

          }
	 
	    echo "</table>";
	   
		
			if ($searchthisdomain=="") {
					
				echo "<form action=manage.php method=POST>
				<center><input type=submit class=submitlink name=submit value=\"Display Page\">";
				echo "<select name=page_no>";
				for ($i=1; $i<=$maxpage+1 ; $i++) {
				echo "<option value=".$i.">Page no: ".$i;
				}
				echo "</center></form>";

			}
			

        }



	//DOMAIN REMOVAL
	function remove_domain_step1(){
		
	        	
		$domainname=$_POST['domainname'];
                echo"<p>";
		echo "<center><form method=post action=manage.php>";
		echo "<table><input type=hidden name=domainname value=".$domainname.">";
                echo "<tr><td class=topbar><center>Are you %100 sure to <font color=red><b>remove</b></font>          <font size=+2> ".$domainname." </font>?</b></center> </td></tr>"; 
	echo "<tr><td class=list1><center><input type=submit class=submitlink name=submit value=removeforever>|<input type=submit class=submitlink name=submit value=no></center></td></tr>";
                echo "</table></form></center>";
        }


	

	//Function to add mass records into zones
	function add_mass_record(){
	require('settings.php');

	//Get domain names
 	$q="select id,domainname from ".DOMAIN_TABLE." ";
	$result= mysql_query($q,$this->connection);

	  while($rrs=mysql_fetch_assoc($result)) {	
	  
	        $q_mass="insert into " .RRS_TABLE. " VALUES('','$rrs[id]','$rrs[domainname]','$_POST[ttl]','$_POST[rrvar]','$_POST[rrtype]','$_POST[priority]','$_POST[rrvalue]','','','')";
	        $result_mass=mysql_query($q_mass,$this->connection);

		//Update serial to make effect
		$this->update_serial($rrs[domainname]);

		
		}	
		
		echo "<center><td class=topbar>Mass update is made for all domain names </td></center>";

	
        }
	
	
        //Function to change TTL values
	function change_ttl(){
		require('settings.php');
                if ($_POST[domainname] == "") {
		//All TTLs will be updated
		 $q="update ".DOMAIN_TABLE." set default_ttl='$_POST[newttl]' ";
		 mysql_query($q,$this->connection);
		 echo "TTL values for all domains are updated";
		
		//Lets tell bind that there is a change
		$q_ttl="SELECT domainname from ".DOMAIN_TABLE."" ;
		$result=mysql_query($q_ttl,$this->connection);
		
		   while ($dom=mysql_fetch_assoc($result)) {
		        $this->update_serial($dom[domainname]);
		   }
		
		
		} 
		
		else
		
		{
		    $q="update ".DOMAIN_TABLE." set default_ttl='$_POST[newttl]' where domainname='$_POST[domainname]' ";
		    mysql_query($q,$this->connection);
		    $this->update_serial($_POST[domainname]);
		    echo "<center>TTL value is set as <b>$_POST[newttl]</b> for <b>$_POST[domainname]</b> domain.</center>";
		}
  
		

          }

	
        function remove_domain_step2(){
		require('settings.php');
		$domainname=$_POST['domainname'];
        	$q="UPDATE ".DOMAIN_TABLE." set status='3' where domainname='$domainname'";
     
	
	       //Check client dns servers, if there is any, update their status fields too
	       $client_no=count($dns_client);
	       if ( count($dns_client) != 0 ) {

                      for ($i=0 ; $i<= $client_no ; $i++) {
                           $q_client="UPDATE ".DOMAIN_TABLE." set ".$dns_client[$i]."_status='3' where domainname='$domainname'";
			   mysql_query($q_client,$this->connection);
			  
			}
																	                }

																			
	
	
	$result= mysql_query($q,$this->connection);

	echo "<center><table cellpadding=0 cellspacing=0>";
	echo "<td class=topbar><center><b>$domainname</b> is marked for removal <a href=manage.php>Goto Index</a></center></td></table></center>";
        }


	//Lets display domainname RRSET
	function display_rrs($domainname){
		$q="select * from ".RRS_TABLE." where domainname='$domainname'";
		$result= mysql_query($q,$this->connection);
		
		$no=mysql_num_rows($result);
	
		if ($no==0) {
			echo "<center><table cellspacing=0 cellpadding=0>
				<tr>
				  <td class=list1><center> No record has been found matching your criteria!</center></td>
				  </tr>
			      </table></center>";
			      exit();
			      
			}

		
		echo "<center>
		<form action=manage.php method=POST>
		<table cellspacing=0 cellpadding=0>
		<tr>
		<td class=header>".$domainname."</td><td class=header>TYPE</td><td class=header >Value </td></tr>
		";
		while($rrs=mysql_fetch_assoc($result)){
			
			echo"<tr><td class=list1>".$rrs['rrvar']."</td><td class=list1>".$rrs['rrtype']."</td><td class=list1>".$rrs['rrvalue']."</td></tr>";
		}
	        echo "
		<input type=hidden name=step value=1>
		<input type=hidden name=domainname value=$domainname>";
		echo "</table>
		<br>	
		<tr><td class=stats1><center><input class=button type=submit name=submit value=Updatedomain></td></center></tr>	
		</form></center>";



		
	}


	 function display_secdns($domainname){
                $q="select * from ".SECDNS_TABLE." where domainname='$domainname'";
                $result= mysql_query($q,$this->connection);

                $no=mysql_num_rows($result);

                if ($no==0) {
                        echo "<center><table cellspacing=0 cellpadding=0>
                                <tr>
                                  <td class=list1><center> No record has been found matching your criteria!</center></td>
                                  </tr>
                              </table></center>";
                              exit();

                        }


                echo "<center>
                <form action=secdns.php method=POST>
                <table cellspacing=0 cellpadding=0>
                <tr>
                <td class=header>".$domainname."</td><td class=header >Value </td></tr>
                ";
                while($rrs=mysql_fetch_assoc($result)){

                        echo"<tr><td class=list1>master dns</td><td class=list1>".$rrs['masterdns']."</td></tr>";
                }
                echo "
                <input type=hidden name=step value=1>
                <input type=hidden name=domainname value=$domainname>";
                echo "</table>
                <br>
                <tr><td class=stats1><center><input class=button type=submit name=submit value=Updatedomain></td></center></tr>
                </form></center>";




        }




	//Function updating serial number and status field too
	function update_serial($domainname) {
        require('settings.php');	
	$q_increment="select serial from ".DOMAIN_TABLE." where domainname='$domainname'";
                        $result=mysql_query($q_increment,$this->connection);
                        $row=mysql_fetch_assoc($result);

                        //Check that, this the first update for this domain or not

                        //Now get first 8 characters from the serial
                        $serial_prefix=substr($row[serial],0,8);
                        $today_prefix=date(Ymd);

                        if ($serial_prefix!=$today_prefix){
                                //Aha.. this is the first update we get it.
                                $serial_prefix=$today_prefix;
                                $last_two_digits="00";
                                }
                        else
                                {
                                //This is not the first update today
                                //Get last two digits then
                                $last_two_digits=substr($row[serial],-2);
                                }


                        //Allow 99 updates per day
                        if ($last_two_digits == 99 ) {
                                echo "<center><table>";
                                echo "<td class=topbar><center>Only <font color=red>99</font> updates are allowed per domain</center></td>
                          </table></center>\n";
                                exit();
                                }

                        //Increment last digit by 1
                        $last_two_digits++;
                        //New number can be less than 10 if so, add a leading 0.
                        if ($last_two_digits < 10 ) {
                                $last_two_digits="0".$last_two_digits;
                                }


                        $new_serial=$serial_prefix.$last_two_digits;
			//Now update serial number
                        $q_serial_update="update ".DOMAIN_TABLE." set serial='$new_serial' where domainname='$domainname'";
                        $result=mysql_query($q_serial_update,$this->connection);

			//Updating serial is not enough alone. We are not editing manually, 
                       //we have to update status field too.
		
			$q="UPDATE ".DOMAIN_TABLE." set status='2' where domainname='$domainname'";
                        mysql_query($q,$this->connection);


                        //Check client dns servers, if there is any, update their status fields too
                        $client_no=count($dns_client);
                         if ( count($dns_client) != 0 ) {

                            for ($i=0 ; $i<= $client_no ; $i++) {
                             $q_client="UPDATE ".DOMAIN_TABLE." set ".$dns_client[$i]."_status='2' where domainname='$domainname'";
                             mysql_query($q_client,$this->connection);
                                     }
                         }
	
		

	



	}
	

	function edit_rrs($domainname,$step){
		require ('settings.php');
	        require('lang.php');
		//Get domain id
	                $q_id="select id,domainname,status from ".DOMAIN_TABLE." where domainname='$domainname' LIMIT 1";
			$id_result=mysql_query($q_id,$this->connection);
			$id_row=mysql_fetch_assoc($id_result);
			$domain_id=$id_row[id];
			
			//Dont allow any update on a newly added domain. This can cause some problems.
		        //Domain must be added into system before any update
			if ($id_row[status]==1) {
				 echo "<tr><td class=list1>$edit_rrs1</td>\n";
				 exit();
			}
			

	//FIRST IF BLOCK
		if ($step == 1) {
		//Only show edit page
			$q="select * from ".RRS_TABLE." where domainname='$domainname'";
                	$result= mysql_query($q,$this->connection);
                	echo "<center>
			<form action=manage.php method=POST>
			<table cellspacing=0 cellpadding=0>
			<tr>
			<td class=header>RecordID</td><td class=header style='border-left: 2px solid #AAAAAA; padding: 3px;'><center>Domain:</center></td><td class=header>---></td><td class=header><center><b>".$domainname."</b></center></td><td class=header>remove</td></tr>
                	";
                	while($rrs=mysql_fetch_assoc($result)){

                        	echo"
		  <tr><td class=list1>".$rrs['rr_no']."</td><td class=list1><input type=text name=rrvar$rrs[rr_no] value=".$rrs['rrvar']." class=forminput></td><td class=list1>";
		  //RRTYPE selection to be able to print priority
		  if ($rrs[rrtype] == "MX") 
		  	{ echo "$rrs[rrtype] $rrs[priority] ";} 
		  else  if ($rrs[rrtype] == "SRV") 
		  	{ echo "$rrs[rrtype] $rrs[priority] ";} 
		  else
		  	{ echo "$rrs[rrtype]" ; }   
		  
		  



		  echo "
		  </td><td class=list1><input type=text name=rrvalue$rrs[rr_no] value='".$rrs['rrvalue']."'></td><td class=list1><center><input type=checkbox name=remove$rrs[rr_no] value=on></center></td></tr>";
                	}
                	echo "
			<input type=hidden name=domainname value=$domainname>
			<input type=hidden name=step value=2>";
                	echo "
			
		  <tr><td class=list1>New ID</td><td class=list1><input type=text name=newrrvar></td><td class=list1><select name=newrrtype>
		  									  <option value=A>A
											  <option  value=MX0>MX 0
											  <option  value=MX1>MX 1
 							                                  <option  value=MX5>MX 5
                                        						  <option  value=MX10>MX 10
                                        						  <option  value=MX20>MX 20
											  <option value=CNAME>CNAME
											  <option value=TXT>TXT
											  <option value=NS>NS
											  <option value=SRV_5_0_5269>SRV 5 0 5269
											  <option value=SRV_20_0_5269>SRV 20 0 5269

											  
											  </select>    
		   </td><td class=list1><input type=text name=newrrvalue><td class=list1><center>New</center></td></tr>

			</table>
			<br>
			<tr border=0><td></td><td><center><input type=submit class=button name=submit value=Updatedomain></td></center></tr>

										
			</form></center>";
		}

		//SECONF IF BLOCK means, record will be really updated:)

		if ($step == 2) {
			// ------------------------------------
			
		
			foreach ( $_POST as $key => $value ) {
				
				//Stop at submit inputreally,  it feeds value 2 and breaks the code
				if ($value == "2") { continue ; } ;
				
				//Fetch rr_no
				$rrvar_no=str_replace("rrvar","",$key);
				$rrvalue_no=str_replace("rrvalue","",$key);
			        $rrremove_no=str_replace("remove","",$key);
				
				//Fetch key name decide which field is coming
				$rrvar_key=strpos($key,"rrvar");
				$rrvalue_key=strpos($key,"rrvalue");
				$rrremove_key=strpos($key,"remove");


				
				if ( $rrvar_key !== false ) {
					//This means it is the rrvar field(the first) comes from post
					$q="UPDATE ".RRS_TABLE." set rrvar='$value' where rr_no='$rrvar_no'";
					mysql_query($q,$this->connection);
				}
				if ( $rrvalue_key !== false ) {
					//rrvalue field comes from post(the second field)
					$q="UPDATE ".RRS_TABLE." set rrvalue='$value' where rr_no='$rrvalue_no'";
					mysql_query($q,$this->connection);
				
				}
				if ( $rrremove_key !== false)  {
					//We are loosing a record:(
					$q="delete from ".RRS_TABLE." where rr_no='$rrremove_no'";
				        mysql_query($q,$this->connection);	
				}
					
				

				
			}
			

			//increment SERIAL number of the domain by 1 and update status
			
			$this->update_serial($domainname,$dns_client);
			
			//UPDATE SERIAL FINISH 
		

			//IF there is a new record added, insert it 
			if ($_POST[newrrvar]!="" && $_POST[newrrvalue]!=""){
			$rrvar=$_POST[newrrvar];
			$rrvalue=$_POST[newrrvalue];
			$rrtype=$_POST[newrrtype];

                if ($rrtype == "MX0") {
                $priority = 0 ;
                $rrtype="MX";
                }
		if ($rrtype == "MX1") {
                $priority = 1 ;
                $rrtype="MX";
                }
                if ($rrtype == "MX5") {
                $priority = 5 ;
                $rrtype= "MX";
                }
                if ($rrtype == "MX10") {
                $priority = 10 ;
                $rrtype= "MX";
                }
                if ($rrtype == "MX20") {
                $priority = 20 ;
                $rrtype= "MX";
                }
                if ($rrtype == "SRV_5_0_5269") {
                $priority = "5 0 5269" ;
                $rrtype= "SRV";
                }
               	if ($rrtype == "SRV_20_0_5269") {
                $priority = "20 0 5269" ;
                $rrtype= "SRV";
                }

	
			
			$q_insert_new_row="INSERT INTO ".RRS_TABLE." (rr_no,id,domainname,rrvar,rrtype,priority,rrvalue) VALUES ('','$domain_id','$domainname','$rrvar','$rrtype','$priority','$rrvalue')";
			mysql_query($q_insert_new_row,$this->connection);

				
				
				}
			
		
			echo"<center><table cellspacing=0 cellpadding=0>";	
			echo "<tr><td class=topbar>Resource Records for <b>$domainname</b> are updated</td><td class=topbar><a href=manage.php>Goto INDEX</a></td></tr></table></center>";
					

		} 
		
		
	}
		
		
	


}

		



$database = new MySQL_db;

?>
